import Vue from 'vue'
import Vuex, { Store } from 'vuex'
import state from '@/store/state'
import mutations from '@/store/mutations'
import getters from '@/store/getters'

Vue.use(Vuex)
// 插件
const myPlugin = store => {
  // 当 store 初始化后调用
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    // mutation 的格式为 { type, payload }
    localStorage.setItem('cart', JSON.stringify(state.cart))
  })
}
export default new Store({
  plugins: [myPlugin],
  state,
  mutations,
  getters
})
